package com.huashizhaoxi.lionapp.dao;

import com.huashizhaoxi.lionapp.entity.Address;
import com.huashizhaoxi.lionapp.entity.User;
import lombok.Getter;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface UserMapper {

    @Select("SELECT * FROM user")
    List<User> getAllUser();

    @Select("SELECT * FROM user WHERE open_id = #{open_id}")
    User getUserByOpenId(String username);

    @Insert("INSERT INTO user(open_id,role,nickname) VALUES(#{open_id},#{role},#{nickname})")
    int addUser(User user);

    @Update("UPDATE user SET nickname=#{nickname},personality_front=#{personality_front},email=#{email},phone=#{phone} WHERE open_id=#{open_id}")
    int modifyUser(User user);

    @Select("SELECT * FROM user_address WHERE user_id = #{id}")
    List<Address> getAllAddress(int id);

    @Insert("INSERT INTO user_address(user_id,address,phone,name,area) VALUES(#{user_id},#{address},#{phone},#{name},#{area})")
    int addAddress(Address address);

    @Update("UPDATE user_address SET address=#{address},phone=#{phone},name=#{name},area=#{area} WHERE id=#{id} ORDER BY id DESC")
    int modifyAddress(Address address);

    @Select("SELECT id, nickname, email, phone, role, create_time, personality_front, avatar FROM user WHERE nickname LIKE CONCAT('%',#{text},'%') ORDER BY role DESC ,id DESC")
    List<User> searchUser(String text);

    @Update("UPDATE user SET role=#{role},nickname=#{nickname} WHERE id=#{id}")
    int changeRole(User user);

    @Select("SELECT * FROM user WHERE id = #{id}")
    User getUserById(int id);
}
